var util = require('../../libs/util.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var that = this;
    // wx.showLoading({
    //   title: '请稍候...',
    // });
    //更新数据
    that.setData({
      driver_id: e.id,
      is_fleet: app.globalData.role_default == 'fleet' ? true : false
    });
    wx.setNavigationBarTitle({ "title": "司机档案" });

    util.getData("/wechat/driver/getDriverDetail", { driver_id: that.data.driver_id }, function (res) {
      console.log(res.data)
      if (res.data.success) {
        var driver_data = res.data.data; //车辆基本信息
        wx.setNavigationBarTitle({ "title": "司机档案--" + driver_data.name });
        that.setData({
          driver_data: driver_data
        });
      }
    });
  },
  showPictrue: function (e) {
    var _image = [];
    var image = e.currentTarget.dataset.image;
    if(typeof image == 'string'){
      _image.push(image);
    }else{
      _image = image;
    }
    wx.previewImage({
      urls: _image
    })
  },
  setCompany: function (e) {
    var id = e.currentTarget.id;
    wx.redirectTo({
      url: '/pages/fleet/selectCompany?type=driver&id=' + id
    })
  },
  verifyDriver: function (e) {
    var id = e.currentTarget.id;
    var driverData = this.data.driverData, is_verify = false, name = '';
    for (var i in driverData) {
      if (driverData[i].ID == id && driverData[i].is_verify == true) {
        is_verify = true;
        name = driverData[i].name;
        break;
      }
    };
    if (is_verify) {
      wx.showModal({
        title: '司机审核',
        content: '司机（' + name + '）已审核',
        showCancel: false,
        success: function (res) {
        }
      })
      return false;
    } else {
      wx.showModal({
        title: '司机审核',
        content: '是否确认司机审核通过',
        success: function (res) {
          if (res.confirm) {
            wx.showLoading({
              title: '请稍候...',
            });
            util.putData("verifyDriver", {
              id: id
            }, function (res, error) {
              console.log(res); console.log(error)
              wx.hideLoading();
              if (error) {
                wx.showToast({
                  title: '审核失败',
                  image: '/images/wrong.png',
                  duration: 3000
                });
                return false;
              }
              if (res.data.success) {
                wx.showToast({
                  title: "审核通过",
                  icon: 'success',
                  success: function () {
                    setTimeout(function () {
                      wx.redirectTo({
                        url: '/pages/fleet/driver'
                      })
                    }, 3000);
                  }
                });
              } else {
                wx.showToast({
                  title: '审核失败',
                  image: '/images/wrong.png',
                  duration: 3000
                });
                return false;
              }
            });
          } else if (res.cancel) {
          }
        }
      })
    }
  },
  deleteDriver: function (e) {
    var id = e.currentTarget.id;
    wx.showModal({
        title: '温馨提示',
        content: '是否删除司机？',
        success: function (res) {
          if (res.confirm) {
            wx.showLoading({
              title: '删除中...',
            });
            util.putData("deleteDriver", {
              id: id
            }, function (res, error) {
              console.log(res); console.log(error)
              wx.hideLoading();
              if (error) {
                wx.showToast({
                  title: '删除失败',
                  image: '/images/wrong.png',
                  duration: 3000
                });
                return false;
              }
              if (res.data.success) {
                wx.showToast({
                  title: "删除通过",
                  icon: 'success',
                  success: function () {
                    setTimeout(function () {
                      wx.redirectTo({
                        url: '/pages/fleet/driver'
                      })
                    }, 3000);
                  }
                });
              } else {
                wx.showToast({
                  title: '删除失败',
                  image: '/images/wrong.png',
                  duration: 3000
                });
                return false;
              }
            });
          } else if (res.cancel) {
          }
        }
      })
  },
  callDriver: function (e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.phone
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})